package selector

import (
	"errors"
	"lnzw/common/math"
	"lnzw/common/proto/common_gen"
	"lnzw/engine/cluster"
	"lnzw/server/logic/node"
)

var (
	//没有可选择的服务器
	ERR_NONE_SERVERS = errors.New("none servers")
)

type ISelector interface {
	Select() (string, error)
}

type RandomSelector struct {
}

var DEFAULT ISelector = &RandomSelector{}

//var TempGates = []string{"127.0.0.1:8080", "localhost:8080", "0.0.0.0:8080"}

func (selector *RandomSelector) Select() (string, error) {
	nodes := cluster.DEFAULT.GetNodes(int32(common_gen.ServerType_gate))
	length := len(nodes)
	if length == 0 {
		return "", ERR_NONE_SERVERS
	}
	index := math.RandBetween(0, len(nodes))
	n := nodes[index]
	return n.(*node.GateNode).Data.(*node.GateNodeData).Addr, nil
}
